<?php
/**
 * Created by PhpStorm.
 * User: SYSTEM
 * Date: 2016/6/28
 * Time: 3:06
 * 商品控制类
 */
namespace Admin\Controller;

use Admin\Model\AuthGroupModel;
use Think\Page;

/**
 * 后台内容控制器
 * @author huajie <banhuajie@163.com>
 */
class GoodController extends AdminController
{

    public function panicproduct(){
        $where['type'] = '1';
        $uid = $_SESSION['onethink_admin']['user_auth']['uid'];
        if($uid != '1'){
            $where['supplier'] = $uid;
        }
        $goodinfo = M("Good")->where($where)->order("create_time desc")->select();
        $goodinvest = array();
        $investlist = M("Invest")->where(array("status"=>"1"))->select();
        if(is_array($investlist)){
            foreach($investlist as $key=>$val){
                $good_info = json_decode($val['good_info'],true);
                if(!is_array($good_info)){
                    $good_info = json_decode($good_info,true);
                }
                foreach($good_info as $k=>$v){
                    $goodinvest[$v["good_id"]] += $v['count'];
                }
            }
        }
        $list = array();
        foreach($goodinfo as $key=>$val){
            $val['investcount'] = $goodinvest[$val['id']];
            if($val['status']==1){
                $val['status'] = "正常";
            }else{
                $val['status'] = "下架";
            }
            $list[$key] = $val;
        }
        $this->assign('_list', $list);
        $this->display();
    }
    public function madeproduct(){
        $where['type'] = '3';
        $uid = $_SESSION['onethink_admin']['user_auth']['uid'];
        if($uid != '1'){
            $where['supplier'] = $uid;
        }
        $goodinfo = M("Good")->where($where)->order("create_time desc")->select();
        $goodinvest = array();
        $investlist = M("Invest")->where(array("status"=>"1"))->select();
        if(is_array($investlist)){
            foreach($investlist as $key=>$val){
                $good_info = json_decode($val['good_info'],true);
                if(!is_array($good_info)){
                    $good_info = json_decode($good_info,true);
                }
                foreach($good_info as $k=>$v){
                    $goodinvest[$v["good_id"]] += $v['count'];
                }
            }
        }
        $list = array();
        foreach($goodinfo as $key=>$val){
            $val['investcount'] = $goodinvest[$val['id']];
            if($val['status']==1){
                $val['status'] = "正常";
            }else{
                $val['status'] = "下架";
            }
            $list[$key] = $val;
        }
        $this->assign('_list', $list);
        $this->display();
    }
    public function orderproduct(){
        $where['type'] = '2';
        $uid = $_SESSION['onethink_admin']['user_auth']['uid'];
        if($uid != '1'){
            $where['supplier'] = $uid;
        }
        $goodinfo = M("Good")->where($where)->order("create_time desc")->select();
        $goodinvest = array();
        $investlist = M("Invest")->where(array("status"=>"1"))->select();
        if(is_array($investlist)){
            foreach($investlist as $key=>$val){
                $good_info = json_decode($val['good_info'],true);
                if(!is_array($good_info)){
                    $good_info = json_decode($good_info,true);
                }
                foreach($good_info as $k=>$v){
                    $goodinvest[$v["good_id"]] += $v['count'];
                }
            }
        }
        $list = array();
        foreach($goodinfo as $key=>$val){
            $val['investcount'] = $goodinvest[$val['id']];
            if($val['status']==1){
                $val['status'] = "正常";
            }else{
                $val['status'] = "下架";
            }
            $list[$key] = $val;
        }
        $this->assign('_list', $list);
        $this->display();
    }
    public function panicadd()
    {
        $fileType = I('file_type', '', '/^\d+$/');
        $id = $_GET['id'];
        $this->assign("categoryinfo",0);
        $this->assign("priceinfo",0);
        $uid = $_SESSION['onethink_admin']['user_auth']['uid'];
        if($uid != '1'){
            $a['supplier'] = $uid;
            $this->assign("goodinfo",$a);
        }
        if($id){
            $goodinfo = M("Good")->where(array("id"=>$id))->find();

            $freeprovince = $goodinfo['freeprovince']?$goodinfo['freeprovince']:0;
            $this->assign("freeprovince",$freeprovince);

            if($goodinfo['img_path']){
                $goodinfo['img_path_p'] = json_encode(explode(",",$goodinfo['img_path']));
            }else{
                $goodinfo['img_path_p'] = 0;
            }
            $arrinfo = $this->getcategoryinfo($id);

            $this->assign("goodinfo",$goodinfo);
            if(count($arrinfo['categoryprice'])){
                $categoryprice = json_encode($arrinfo['categoryprice']);
            }else{
                $categoryprice = 0;
            }
            $this->assign("priceinfo",$categoryprice);

            if(count($arrinfo['categoryinfo'])){
                $categoryinfo = json_encode($arrinfo['categoryinfo']);
            }else{
                $categoryinfo = 0;
            }
            $this->assign("categoryinfo",$categoryinfo);
        }
        !$fileType && $this->error('参数错误');
        $this->assign('_title', '新增文件');
        $this->assign('fileLimit', file_limit($fileType));
        $this->assign('file_type', $fileType);
        if($fileType == 1){
            $fileTypename = "抢购商品";
        }elseif($fileType == 2){
            $fileTypename = "预约商品";
        }else{
            $fileTypename = "定制商品";
        }
        $this->assign('file_type_name', $fileTypename);
        $this->display();
    }
    //删除商品
    public function deletegood(){
        $id = $_GET['id'];
        if(empty($id)){
            $this->error('参数错误');
        }else{
            $goodinfo = M("Good")->where(array("id"=>$id))->delete();
            $this->success('删除成功');
        }
    }
    public function addGood()
    {

        $data = array();
        $data['type'] = $_POST['type'];
        $data['title'] = $_POST['title'];
        $data['sub_title'] = $_POST['sub_title'];
        $data['info'] = $_POST['info'];
        $data['range'] = '10';

        $data['status'] = $_POST['status'];
        $data['create_time'] = date("Y-m-d H:i:s");
        $data['purchase_num'] = $_POST['purchase_num'];
        $data['production_time'] = $_POST['production_time'];
        $data['area'] = $_POST['city'];
        $data['city'] = $_POST['area'];
        $data['province'] = $_POST['province'];
        if(empty($_POST['img_path'])){
            $this->error('请上传图片');
        }
        $img_path = implode(",",array_filter(explode(",",$_POST['img_path'])));
        $data['img_path'] = $img_path;
        $data['status'] = $_POST['status'];
        $data['original_price'] = $_POST['original_price'];
        $data['discount_price'] = $_POST['discount_price'];
        $data['stock'] = $_POST['stock'];
        $data['appointment'] = $_POST['appointment'];
        $data['end_time'] = $_POST['end_time'];
        $data['freeprovince'] = implode(",",$_POST['freeprovince']);
        $data['postage'] = $_POST['postage'];
        $data['supplier'] = $_POST['supplier'];
        $priceinfo = $_POST['priceinfo'];
        $info = $_POST['info'];

        if($_POST['good_id']>0){
            M("Good")->where(array("id"=>$_POST['good_id']))->save($data);
            $goodid = $_POST['good_id'];

            $data = array();
            $data['good_id'] = $goodid;
            $categoryname = $_POST["category_name"];
            $categoryvalue = $_POST["category_value"];
            if(is_array($categoryname)){
                $categorylist = array();
                foreach($categoryname as $key=>$val){
                    if($val == ""){
                        continue;
                    }
                    if(count($categoryvalue[$val])){
                        foreach($categoryvalue[$val] as $k=>$v){
                            if($v == ""){
                                continue;
                            }
                            $data = array();
                            $data['name'] = $val;
                            $data["good_id"] = $goodid;
                            if(isset($v['value'])){
                                $data['value'] = $v['value'];
                            }else{
                                $data['value'] = $v;
                            }
                            $data['create_time'] = date("Y-m-d H:i:s");
                            if(isset($v['id'])){
                                M("GoodCategory")->where(array("id"=>$v['id']))->save($data);
                                $categorylist[$v['value']] = $v['id'];
                            }else{
                                $categorylist[$v] = M("GoodCategory")->add($data);
                            }
                        }
                    }
                }
                foreach($priceinfo as $ks=>$vs){
                    $ks = str_replace('"',"",$ks);
                    $c_n = explode(",",$ks);
                    $c_n = array_filter($c_n);

                    $category_id = "";
                    foreach($c_n as $kn=>$vn){
                        $category_id .= $categorylist[$vn].",";
                    }
                    $category_id = substr($category_id,0,-1);
                    $data = array();
                    $data['good_id'] = $goodid;
                    $data['category_id'] = $category_id;
                    $data['original_price'] =$vs['original_price'][0];
                    $data['discount_price'] = $vs['discount_price'][0];
                    $data['stock'] =$vs['stock'][0];
                    $data['code'] = $vs['code'][0];
                    $data['create_time'] = date("Y-m-d H:i:s");
                    $res = M("GoodPrice")->where(array("category_id"=>$category_id))->find();
                    if($res){
                        M("GoodPrice")->where(array("category_id"=>$category_id))->save($data);
                    }else{
                        M("GoodPrice")->add($data);
                    }
                }
            }
            $info = '<meta charset="utf8"><style>  h1{ font-size:30px; } h2{ font-size:25px; } h3{ font-size:20px; } </style>'.$info;
            filewrite("./Public/static/App/goodinfo".$goodid.".html",$info);
            $this->success('修改成功');
        }else{
            $goodid = M("Good")->add($data);
            $data = array();
            $data['good_id'] = $goodid;
            $categoryname = $_POST["category_name"];
            $categoryvalue = $_POST["category_value"];
            if(is_array($categoryname)){
                $categorylist = array();
                foreach($categoryname as $key=>$val){
                    if($val == ""){
                        continue;
                    }
                    if(count($categoryvalue[$val])){
                        foreach($categoryvalue[$val] as $k=>$v){
                            if($v == ""){
                                continue;
                            }
                            $data = array();
                            $data['name'] = $val;
                            $data["good_id"] = $goodid;
                            $data['value'] = $v;
                            $data['create_time'] = date("Y-m-d H:i:s");
                            $categorylist[$v] = M("GoodCategory")->add($data);
                        }
                    }
                }

                foreach($priceinfo as $ks=>$vs){
                    $ks = str_replace('"',"",$ks);
                    $c_n = explode(",",$ks);
                    $c_n = array_filter($c_n);

                    $category_id = "";
                    foreach($c_n as $kn=>$vn){
                        $category_id .= $categorylist[$vn].",";
                    }
                    $category_id = substr($category_id,0,-1);
                    $data = array();
                    $data['good_id'] = $goodid;
                    $data['category_id'] = "$category_id";
                    $data['original_price'] =$vs['original_price'][0];
                    $data['discount_price'] = $vs['discount_price'][0];
                    $data['stock'] =$vs['stock'][0];
                    $data['code'] = $vs['code'][0];
                    $data['create_time'] = date("Y-m-d H:i:s");
                    M("GoodPrice")->add($data);
                }
            }

            $info = '<meta charset="utf8"><style>  h1{ font-size:30px; } h2{ font-size:25px; } h3{ font-size:20px; } </style>'.$info;
            filewrite("./Public/static/App/goodinfo".$goodid.".html",$info);
            $this->success('新增成功');
        }
    }
    public function addcategory(){

        parse_str($_POST['data'],$arr);
        $goodid = $_POST['id'];
        $categoryname = $arr["category_name"];
        $categoryvalue = $arr["category_value"];

        $categorylist = array();
        foreach($categoryname as $key=>$val){
            if($val == ""){
                continue;
            }
            if(count($categoryvalue[$key])){
                foreach($categoryvalue[$key] as $k=>$v){
                    if($v == ""){
                        continue;
                    }
                    $data = array();
                    $data['name'] = $val;
                    $data["good_id"] = $goodid;
                    if(isset($v['value'])){
                        $data['value'] = $v['value'];
                    }else{
                        $data['value'] = $v;
                    }
                    $data['create_time'] = date("Y-m-d H:i:s");
                    if(isset($v['id'])){
                        M("GoodCategory")->where(array("id"=>$v['id']))->save($data);
                        $categorylist[$v['value']] = $v['id'];
                    }else{
                        $categorylist[$v] = M("GoodCategory")->add($data);
                    }
                }
            }else{
                foreach($categoryvalue[$val] as $k=>$v){
                    if($v == ""){
                        continue;
                    }
                    $data = array();
                    $data['name'] = $val;
                    $data["good_id"] = $goodid;
                    if(isset($v['value'])){
                        $data['value'] = $v['value'];
                    }else{
                        $data['value'] = $v;
                    }
                    $data['create_time'] = date("Y-m-d H:i:s");
                    $categorylist[$v] = M("GoodCategory")->add($data);
                }
            }
        }
        $arrinfo = $this->getcategoryinfo($goodid);
        echo json_encode($arrinfo);
    }
    function getcategoryinfo($goodid){
        if($goodid == ""){
            return false;
        }
        $categorylist = M("GoodCategory")->where(array("good_id"=>$goodid))->select();
        $categoryinfo = array();
        $categoryps = array();
        if(is_array($categorylist)){
            foreach($categorylist as $key=>$val){
                $categoryinfo[$val['name']][] = array($val['value'],$val['id']);
                $categorydata[$val['id']] = array("name"=>$val['name'],"value"=>$val['value']);
                $categoryps[$val['name']][] = $val['id'];
            }
        }

        if(count($categoryps) == 1){
            $n = array();
            foreach(reset($categoryps) as $ks=>$vs){
                $c = array();
                $c[] = $vs;
                $n[] = $c;
            }

        }elseif(count($categoryps)==2){
            $n = [];
            $cate1 = reset($categoryps);
            $cate2 = next($categoryps);
            for($i=0;$i<count($cate1);$i++){
                for($j=0;$j<count($cate2);$j++){
                    $c = array();
                    $c[] = $cate1[$i];
                    $c[] = $cate2[$j];
                    $n[] = $c;
                }
            }
        }elseif(count($categoryps)==3){
            $n = [];
            $cate1 = reset($categoryps);
            $cate2 = next($categoryps);
            $cate3 = next($categoryps);
            for($i=0;$i<count($cate1);$i++){
                for($j=0;$j<count($cate2);$j++){
                    for($s=0;$s<count($cate3);$s++){
                        $c = array();
                        $c[] = $cate1[$i];
                        $c[] = $cate2[$j];
                        $c[] = $cate3[$s];
                        $n[] = $c;
                    }
                }
            }
        }
        $categoryprice = array();
        foreach($n as $key=>$val){
            $categoryid = implode(",",$val);
            $priceinfo = M("GoodPrice")->where(array("good_id"=>$goodid,"category_id"=>$categoryid))->find();
            $ca = "";
            $cal = array();
            foreach($val as $k=>$v){
                $ca .= $categorydata[$v]['value'].",";
                $cal[] = $categorydata[$v]['value'];
            }
            $priceinfo["category_id"] = substr($ca,0,-1);
            $priceinfo["category_array"] = $cal;
            $priceinfo["good_id"] = $goodid;
            $priceinfo["original_price"] = $priceinfo['original_price']?$priceinfo['original_price']:"";
            $priceinfo["discount_price"] = $priceinfo['discount_price']?$priceinfo['discount_price']:"";
            $priceinfo["stock"] = $priceinfo['stock']?$priceinfo['stock']:"";
            $priceinfo["code"] = $priceinfo['code']?$priceinfo['code']:"";
            $categoryprice[$key] = $priceinfo;
        }

        return array("categoryinfo"=>$categoryinfo,"categoryprice"=>$categoryprice);
    }
}